home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / chmod.c < prev    next >
C/C++ Source or Header  |  1994-03-29  |  6KB  |  225 lines

  1. RCS_ID_C="$Id: chmod.c,v 3.1 1994/03/29 12:56:35 ppessi Exp $";
  2. /*
  3.  * chmod.c --- chmod() for usergroup link library
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP User Library.
  8.  *
  9.  * Copyright © 1993,1994 AmiTCP/IP Group, <amitcp-group@hut.fi>
  10.  *             Helsinki University of Technology, Finland.
  11.  *             All rights reserved.
  12.  *
  13.  * This function is based on SetOwner37 code
  14.  * Copyright © 1993 by Geert Uytterhoeven
  15.  * 
  16.  * Created      : Wed Sep 15 02:20:20 1993 ppessi
  17.  * Last modified: Mon Mar 28 02:03:40 1994 ppessi
  18.  *
  19.  */
  20.  
  21. /****** net.lib/chmod *********************************************************
  22.  
  23.     NAME
  24.         chmod, fchmod - change mode of file
  25.  
  26.     SYNOPSIS
  27.         #include <sys/stat.h>
  28.  
  29.         int chmod(const char *path, mode_t mode);
  30.  
  31.         int fchmod(int fd, mode_t mode);
  32.  
  33.     DESCRIPTION
  34.         The function chmod() sets the file permission bits of the file
  35.         specified by the pathname path to mode. Fchmod() sets the permission
  36.         bits of the specified file descriptor fd. Chmod() verifies that the
  37.         process owner (user) either owns the file specified by path (or fd),
  38.         or is the super-user.  A mode is created from or'd permission bit
  39.         masks defined in <sys/stat.h>:
  40.  
  41.               #define S_IRWXU 0000700    \* RWX mask for owner *\
  42.               #define S_IRUSR 0000400    \* R for owner *\
  43.               #define S_IWUSR 0000200    \* W for owner *\
  44.               #define S_IXUSR 0000100    \* X for owner *\
  45.  
  46.               #define S_IRWXG 0000070    \* RWX mask for group *\
  47.               #define S_IRGRP 0000040    \* R for group *\
  48.               #define S_IWGRP 0000020    \* W for group *\
  49.               #define S_IXGRP 0000010    \* X for group *\
  50.  
  51.               #define S_IRWXO 0000007    \* RWX mask for other *\
  52.               #define S_IROTH 0000004    \* R for other *\
  53.               #define S_IWOTH 0000002    \* W for other *\
  54.               #define S_IXOTH 0000001    \* X for other *\
  55.  
  56.               #define S_ISUID 0004000    \* set user id on execution *\
  57.               #define S_ISGID 0002000    \* set group id on execution *\
  58.               #define S_ISVTX 0001000    \* save swapped text even after use *\
  59.  
  60.         The ISVTX (the sticky bit) indicates to the system which executable
  61.         files are shareable (pure).
  62.  
  63.         Writing or changing the owner of a file turns off the set-user-id
  64.         and set-group-id bits unless the user is the super-user.  This makes
  65.         the system somewhat more secure by protecting set-user-id
  66.         (set-group-id) files from remaining set-user-id (set-group-id) if
  67.         they are modified.
  68.  
  69.     RETURN VALUES
  70.         Upon successful completion, a value of 0 is returned.  Otherwise, a
  71.         value of -1 is returned and errno is set to indicate the error.
  72.  
  73.     ERRORS
  74.         Chmod() will fail and the file mode will be unchanged if:
  75.  
  76.         [ENOTDIR]     A component of the path prefix is not a directory.
  77.  
  78.         [ENAMETOOLONG]
  79.                       A component of a pathname exceeded 255 characters, or
  80.                       an entire path name exceeded 1023 characters.
  81.  
  82.         [ENOENT]      The named file does not exist.
  83.  
  84.         [EACCES]      Search permission is denied for a component of the
  85.                       path prefix.
  86.  
  87.         [EPERM]       The effective user ID does not match the owner of the
  88.                       file and the effective user ID is not the super-user.
  89.  
  90.         [EROFS]       The named file resides on a read-only file system.
  91.  
  92.         [EFAULT]      Path points outside the process's allocated address
  93.                       space.
  94.  
  95.         [EIO]         An I/O error occurred while reading from or writing to
  96.                       the file system.
  97.  
  98.         Fchmod() will fail if:
  99.  
  100.         [EBADF]       The descriptor is not valid.
  101.  
  102.         [EINVAL]      Fd refers to a socket, not to a file.
  103.  
  104.         [EROFS]       The file resides on a read-only file system.
  105.  
  106.         [EIO]         An I/O error occurred while reading from or writing to
  107.                       the file system.
  108.  
  109.     NOTES
  110.         This call is provided for Unix compatibility.  It does not know all
  111.         Amiga protection bits (Delete, Archive, Script).  The archive and
  112.         script bits are cleared, Delete set according the Write bit.
  113.  
  114.     SEE ALSO
  115.         open(),  chown(),  stat()
  116.  
  117. *****************************************************************************
  118. */
  119.  
  120. #include <libraries/usergroup.h>
  121. #include <proto/dos.h>
  122.  
  123. #include <sys/types.h>
  124. #include <sys/stat.h>
  125.  
  126. #include "fibex.h"
  127. #include "netlib.h"
  128.  
  129. /*
  130.  * rwx -> rwed
  131.  */
  132. const static BYTE pbits[8] = 
  133.   0, 0X2, 0X5, 0X7, 0X8, 0XA, 0XD, 0XF, 
  134. };
  135.  
  136. int chmod(const char *path, int mode)
  137. {
  138.   LONG prot = 
  139.     (pbits[mode & 7] << FIBB_OTR_DELETE) |
  140.       (pbits[(mode >> 3) & 7] << FIBB_GRP_DELETE) |
  141.     (pbits[(mode >> 6) & 7] ^ 0xf);
  142.  
  143.   if (mode & S_ISVTX)
  144.     prot |= FIBF_PURE;
  145.   if (mode & S_ISUID)
  146.     prot |= FIBF_SUID;
  147.   if (mode & S_ISGID)
  148.     prot |= FIBF_SGID;
  149.  
  150.   if (!SetProtection(path, prot)) {
  151.     set_errno(IoErr());
  152.     return -1;
  153.   } else {
  154.     return 0;
  155.   }
  156. }
  157.  
  158. #ifdef DEBUGGING
  159. #include <proto/usergroup.h>
  160. #include <stdlib.h>
  161. #include <string.h>
  162. #include <exec/execbase.h>
  163. #include <ctype.h>
  164.  
  165. int errno;
  166. extern struct ExecBase *SysBase;
  167.  
  168. void PrintUserFault(LONG code, const UBYTE *banner);
  169.  
  170. const static char usage[] = "usage: chmod [-fR] mode file ...";
  171.  
  172. void main(int argc, char *argv[])
  173. {
  174.   struct Process *p = (struct Process *)SysBase->ThisTask;
  175.   BPTR Stderr = p->pr_CES ? p->pr_CES : p->pr_COS;
  176.  
  177.   short perrors = 1, recursive = 0;
  178.   char *modenum; mode_t mode;
  179.  
  180.   while (argc > 1 && argv[1][0] == '-') {
  181.     switch (argv[1][1]) {
  182.     case 'f':
  183.       perrors = 0;
  184.       break;
  185.     case 'R':
  186.       recursive = 1;
  187.       break;
  188.     default:
  189.       FPrintf(Stderr, usage);
  190.       exit(10);
  191.     }
  192.     argv++, argc--;
  193.   }
  194.  
  195.   if (argc <= 2) {
  196.     FPrintf(Stderr, usage);
  197.     exit(10);
  198.   }
  199.  
  200.   modenum = argv[1]; argv++, argc--;
  201.  
  202.   mode = 0;
  203.   while (isdigit(*modenum)) {
  204.     mode <<= 3;
  205.     mode += *modenum++ - '0';
  206.   }
  207.  
  208.   if (*modenum) {
  209.     FPrintf(Stderr, usage);
  210.     exit(10);
  211.   }
  212.  
  213.   while (argc-- > 1) {
  214.     if (chmod(argv++[1], mode) == -1) {
  215.       if (perrors)
  216.     PrintUserFault(errno, "chmod");
  217.       exit(RETURN_ERROR);
  218.     }
  219.   }
  220.  
  221.   exit(0);
  222. }
  223. #endif
  224.